﻿using nRoute.Components.Composition;
using nRoute.ViewServices.Contracts;
using nRoute.Mapping;
#if !SILVERLIGHT && !WINRT
using System.Linq;
using System.IO;
using Microsoft.Win32;
#endif
#if (WINRT)
#else
using System.Windows.Controls;
#endif

#if (!WINRT)

namespace nRoute.ViewServices.Services
{
    [MapViewService(typeof(IOpenFileViewService), OpenFileViewService.SERVICE_NAME, InitializationMode = InitializationMode.OnDemand, LifetimeManagerType = typeof(PerInstanceLifetimeManager))]
    public class OpenFileViewService 
		 : IOpenFileViewService
    {
        private const string SERVICE_NAME = "nRouteOpenFileViewService"; 
        private const string DEFAULT_FILES_FILTER = "All Files (*.*)|*.*";
        private const int DEFAULT_FILTER_INDEX = 1;
        
#region IOpenFileViewService Members

        public OpenFileResponse OpenFile(bool multiselect)
        {
            return OpenFile(DEFAULT_FILES_FILTER, DEFAULT_FILTER_INDEX, multiselect);
        }

        public OpenFileResponse OpenFile(string filter, int filterIndex, bool multiselect)
        {
            var _openFileDialog = new OpenFileDialog 
                                      {
                                          Filter = filter,
                                          FilterIndex = filterIndex,
                                          Multiselect = multiselect
                                      };

            // note this is blocking
            var _result = _openFileDialog.ShowDialog();

#if SILVERLIGHT
            return !_result.GetValueOrDefault(false) ? new OpenFileResponse(true) :
                new OpenFileResponse(_openFileDialog.File, _openFileDialog.Files);
#else
            return !_result.GetValueOrDefault(false) ? new OpenFileResponse(true) :
                new OpenFileResponse(
                    new FileInfo(_openFileDialog.FileName),
                    from f in _openFileDialog.FileNames 
                    select new FileInfo(f));
#endif

        }

#endregion

    }
}

#endif